package com.idp.common.utils;

import cn.hutool.core.date.DateUtil;
import org.springframework.util.StringUtils;

import java.beans.PropertyEditorSupport;
import java.util.Date;

/**
 * 日期格式转换
 * @author king
 */
public class DateConvertEditor extends PropertyEditorSupport {

	private static String[] parsePatterns = {
			"yyyy-MM-dd", "yyyy-MM-dd HH:mm:ss", "yyyy-MM-dd HH:mm", "yyyy-MM-dd HH", "yyyy-MM",
			"yyyy/MM/dd", "yyyy/MM/dd HH:mm:ss", "yyyy/MM/dd HH:mm", "yyyy/MM/dd HH", "yyyy/MM"};

	@Override
	public void setAsText(String text) throws IllegalArgumentException {

		if (StringUtils.hasText(text)) {

			Date date = DateUtil.parse(text, parsePatterns);

			setValue(date);

		} else {

			setValue(null);
		}
	}

}
